<template>
  <div class="app-container">
    <div class="title-icon">
      <i class="iconfont iconhelp" @click="preview"></i>
    </div>
    <el-row :gutter="20">
      <!--业务类别数据-->
      <el-col :span="5" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入业务类别名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            @node-click="handleNodeClick"
          >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span>{{ node.label }}</span>
              <span class="childrenNum">{{data.childrenNum}}</span>
            </span>
          </el-tree>
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="19" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="流程名称" prop="workflowName">
            <el-input
              v-model="queryParams.workflowName"
              placeholder="请输入流程名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="发布日期" prop="createTime">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:auditWorkflowConfig:add']"
            >新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:auditWorkflowConfig:edit']"
            >修改</el-button>
          </el-col>-->
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:auditWorkflowConfig:remove']"
            >删除</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:auditWorkflowConfig:export']"
            >导出</el-button>
          </el-col>-->
        </el-row>

        <el-table
          stripe
          v-loading="loading"
          :data="auditWorkflowConfigList"
          @sort-change="tableSortChange"
          @selection-change="handleSelectionChange"
          :default-sort="{prop: 'createTime', order: 'descending'}"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column label="id 主键" align="center" prop="id" /> -->
          <el-table-column
            label="显示名称"
            align="left"
            prop="showName"
            :sort-orders="['ascending', 'descending']"
            sortable="custom"
          />
          <el-table-column
            label="流程名称"
            align="left"
            prop="workflowName"
            :sort-orders="['ascending', 'descending']"
            sortable="custom"
          />
          <el-table-column label="业务类别" align="center" prop="businessCategoryList">
            <template slot-scope="{row}">
              <div v-for="(item,index) in row.businessCategoryList" :key="index">{{ item.menuName }}</div>
            </template>
          </el-table-column>
          <el-table-column
            label="发布日期"
            align="center"
            prop="createTime"
            :sort-orders="['ascending', 'descending']"
            sortable="custom"
          />
          <!-- <el-table-column label="流程编码" align="center" prop="workflowCode" /> -->
          <el-table-column label="版本号" align="center" prop="version" />
          <!-- <el-table-column label="发起流程url" align="center" prop="url" /> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:auditWorkflowConfig:edit']"
              >编辑</el-button>
              <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:auditWorkflowConfig:remove']"
              >删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改成果校审流程配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="显示名称" prop="showName">
          <el-input v-model="form.showName" placeholder="请输入显示名称" />
        </el-form-item>
        <el-form-item label="业务类别" prop="businessCategoryIds">
          <treeselect
            v-model="form.businessCategoryIds"
            :options="deptOptionsChild"
            placeholder="请选择业务类别"
            :disable-branch-nodes="true"
            :multiple="true"
          />
        </el-form-item>
        <el-form-item label="流程名称" prop="workflowName">
          <el-select
            v-model="formWorkflowName"
            placeholder="请选择流程名称"
            style="width:100%"
            value-key="guid"
          >
            <el-option
              :label="item.workflowName"
              :value="{guid:item.guid,name:item.workflowName}"
              v-for="item in workFlowNameList"
              :key="item.guid"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="流程编码" prop="workflowCode">
          <el-input v-model="form.workflowCode" placeholder="请输入流程编码" />
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="发起流程url" prop="url">
          <el-input v-model="form.url" type="textarea" placeholder="请输入内容" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAuditWorkflowConfig,
  getAuditWorkflowConfig,
  delAuditWorkflowConfig,
  addAuditWorkflowConfig,
  updateAuditWorkflowConfig,
  exportAuditWorkflowConfig,
  listGetWorkName
} from "@/api/system/auditWorkflowConfig";
import { treeSelectList } from "@/mixins/systemMixin.js";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "AuditWorkflowConfig",
  components: {
    Treeselect,
  },
  mixins: [treeSelectList],
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      pageTempName: 'AUDIT_WORKFLOW_CONFIG',
      // 遮罩层
      loading: true,
      // 选中数组
      guids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 成果校审流程配置表格数据
      auditWorkflowConfigList: [],
      //流程名称
      workFlowNameList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      //新增部门树选项
      deptOptionsChild: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workflowName: undefined,
        businessCategoryId: undefined,
      },
      dateRange: [],
      formWorkflowName: null,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        businessCategoryIds: [
          { required: true, message: "业务类别不能为空", trigger: "blur" }
        ],
        showName: [
          { required: true, message: "显示名称不能为空", trigger: "blur" }
        ],
        workflowName: [
          { required: true, message: "流程名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName (val) {
      this.$refs.tree.filter(val);
    }
  },
  created () {
    this.getList();
    this.getWorkName();
    this.getTreeselect(this.pageTempName);
    this.getTreeSelectChild(this.pageTempName);
  },
  methods: {
    /** 查询成果校审流程配置列表 */
    getList () {
      this.loading = true;
      listAuditWorkflowConfig(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.auditWorkflowConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWorkName () {
      listGetWorkName().then(res => {
        if (res.code === 200) {
          this.workFlowNameList = res.data
        }
      })
    },
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true;
      if (data.label != null) {
        return data.label.indexOf(value) !== -1;
      }
    },
    // 节点单击事件
    handleNodeClick (data) {
      this.queryParams.businessCategoryId = data.id;
      this.getList();
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        guid: undefined,
        businessCategoryIds: undefined,
        workflowName: undefined,
        workflowCode: undefined,
        version: undefined,
        url: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        workflowConfigId: undefined
      };
      this.formWorkflowName = undefined
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.guids = selection.map(item => item.guid);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加成果校审流程配置";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {

      this.reset();
      const guid = row.guid || this.guids;
      getAuditWorkflowConfig(guid).then(response => {
        this.form = response.data;
        this.formWorkflowName = {
          guid: response.data.workflowConfigId,
          name: response.data.workflowName
        }
        this.open = true;
        this.title = "修改成果校审流程配置";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.workflowConfigId = this.formWorkflowName.guid
      this.form.workflowName = this.formWorkflowName.name
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != undefined) {
            updateAuditWorkflowConfig(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.getTreeselect(this.pageTempName);
                this.getTreeSelectChild(this.pageTempName);
              } else {
                this.msgError(response.msg);
              }
            });
          } else {

            addAuditWorkflowConfig(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.getTreeselect(this.pageTempName);
                this.getTreeSelectChild(this.pageTempName);
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const guids = row.guid || this.guids;
      this.$confirm(
        '是否确认删除所有业务类别下的此成果校审流程配置? 如只删除该业务类别，请通过“修改”功能操作?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function () {
          return delAuditWorkflowConfig(guids);
        })
        .then(() => {
          this.getList();
          this.getTreeselect(this.pageTempName);
          this.getTreeSelectChild(this.pageTempName);
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport () {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有成果校审流程配置数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function () {
          return exportAuditWorkflowConfig(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function () { });
    },
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}系统管理/成果校审流程配置.xlsx`
      )}&name=${encodeURIComponent("成果校审流程配置.xlsx")}`;
    },
  }
};
</script>
<style lang="scss" scoped>
.custom-tree-node {
  display: flex;
  flex: 1;
  .childrenNum {
    //flex: 1;
    text-align: right;
  }
}
.title-icon {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  cursor: pointer;
  margin-bottom: 10px;
  .iconhelp {
    font-size: 18px;
  }
}
</style>